<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="baidu-site-verification" content="zD47G9xe42" />


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="uiste个人博客网站,uiste,uiste博客,PHP,运维,Hexo,技术博客" />





  <link rel="alternate" href="/atom.xml" title="uiste" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="Stay Hungry，Stay Foolish">
<meta property="og:type" content="website">
<meta property="og:title" content="uiste">
<meta property="og:url" content="http://blog.uiste.com/page/6/index.html">
<meta property="og:site_name" content="uiste">
<meta property="og:description" content="Stay Hungry，Stay Foolish">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="uiste">
<meta name="twitter:description" content="Stay Hungry，Stay Foolish">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://blog.uiste.com/page/6/"/>

  <title>
 
  uiste - Stay Hungry，Stay Foolish 

</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?b58b87af012832b8ae35e33bb45b44fa";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left 
   page-home 
 ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">uiste</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">uiste个人博客</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      

            <li class="menu-item menu-item-search"> <a title="把这个链接拖到你的Chrome收藏夹工具栏中" href='javascript:(function() {
    function c() {
        var e = document.createElement("link");
        e.setAttribute("type", "text/css");
        e.setAttribute("rel", "stylesheet");
        e.setAttribute("href", f);
        e.setAttribute("class", l);
        document.body.appendChild(e)
    }
 
    function h() {
        var e = document.getElementsByClassName(l);
        for (var t = 0; t < e.length; t++) {
            document.body.removeChild(e[t])
        }
    }
 
    function p() {
        var e = document.createElement("div");
        e.setAttribute("class", a);
        document.body.appendChild(e);
        setTimeout(function() {
            document.body.removeChild(e)
        }, 100)
    }
 
    function d(e) {
        return {
            height : e.offsetHeight,
            width : e.offsetWidth
        }
    }
 
    function v(i) {
        var s = d(i);
        return s.height > e && s.height < n && s.width > t && s.width < r
    }
 
    function m(e) {
        var t = e;
        var n = 0;
        while (!!t) {
            n += t.offsetTop;
            t = t.offsetParent
        }
        return n
    }
 
    function g() {
        var e = document.documentElement;
        if (!!window.innerWidth) {
            return window.innerHeight
        } else if (e && !isNaN(e.clientHeight)) {
            return e.clientHeight
        }
        return 0
    }
 
    function y() {
        if (window.pageYOffset) {
            return window.pageYOffset
        }
        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
    }
 
    function E(e) {
        var t = m(e);
        return t >= w && t <= b + w
    }
 
    function S() {
        var e = document.createElement("audio");
        e.setAttribute("class", l);
        e.src = i;
        e.loop = false;
        e.addEventListener("canplay", function() {
            setTimeout(function() {
                x(k)
            }, 500);
            setTimeout(function() {
                N();
                p();
                for (var e = 0; e < O.length; e++) {
                    T(O[e])
                }
            }, 15500)
        }, true);
        e.addEventListener("ended", function() {
            N();
            h()
        }, true);
        e.innerHTML = " <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>";
        document.body.appendChild(e);
        e.play()
    }
 
    function x(e) {
        e.className += " " + s + " " + o
    }
 
    function T(e) {
        e.className += " " + s + " " + u[Math.floor(Math.random() * u.length)]
    }
 
    function N() {
        var e = document.getElementsByClassName(s);
        var t = new RegExp("\\b" + s + "\\b");
        for (var n = 0; n < e.length; ) {
            e[n].className = e[n].className.replace(t, "")
        }
    }
 
    var e = 30;
    var t = 30;
    var n = 350;
    var r = 350;
    var i = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";
    var s = "mw-harlem_shake_me";
    var o = "im_first";
    var u = ["im_drunk", "im_baked", "im_trippin", "im_blown"];
    var a = "mw-strobe_light";
    var f = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";
    var l = "mw_added_css";
    var b = g();
    var w = y();
    var C = document.getElementsByTagName("*");
    var k = null;
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            if (E(A)) {
                k = A;
                break
            }
        }
    }
    if (A === null) {
        console.warn("Could not find a node of the right size. Please try a different page.");
        return
    }
    c();
    S();
    var O = [];
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            O.push(A)
        }
    }
})()    '>High</a> </li>

    </ul>
  

 
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
   
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/20160916-1.html" itemprop="url">
                  Day3-PHP面向对象
                </a>
              
            
          </h1>
        

        <div class="post-meta">
        
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-09-16T23:41:27+08:00" content="2016-09-16">
              2016-09-16
            </time>
          </span>

          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/20160916-1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="20160916-1.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="clone与-clone"><a href="#clone与-clone" class="headerlink" title="clone与__clone()"></a>clone与__clone()</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">class Person&#123;</div><div class="line">	protected $name;</div><div class="line">	public function __construnt($name)&#123;</div><div class="line">		$this-&gt;name=$name;</div><div class="line">	&#125;</div><div class="line">	public function show()&#123;</div><div class="line">		echo $this-&gt;name;</div><div class="line">	&#125;</div><div class="line">	public function __clone()&#123;</div><div class="line">		echo &quot;对象被克隆了&lt;br&gt;&quot;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$per1 = new Person(&apos;胡浩&apos;);</div><div class="line">var_dump($per1);</div><div class="line">echo &apos;&lt;br&gt;&apos;;</div><div class="line">$per2 = $per1;</div><div class="line">var_dump($per2);</div><div class="line">echo &apos;&lt;br&gt;&apos;;</div><div class="line">$per3 = clone $per1;</div><div class="line">var_dump($per3);</div><div class="line">?&gt;</div><div class="line">object(Person)#1 (1) &#123; [&quot;name&quot;:protected]=&gt; NULL &#125; </div><div class="line">object(Person)#1 (1) &#123; [&quot;name&quot;:protected]=&gt; NULL &#125; </div><div class="line">对象被克隆了</div><div class="line">object(Person)#2 (1) &#123; [&quot;name&quot;:protected]=&gt; NULL &#125;</div></pre></td></tr></table></figure>
<blockquote>
<p>创建新对象的方法有两个：<br>1、    实例化<br>2、    clone<br>当执行clone命令的时候，会自动调用__clone()函数</p>
</blockquote>
<h3 id="单例模式"><a href="#单例模式" class="headerlink" title="单例模式"></a>单例模式</h3><p>一个类只能有一个对象</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">//三私一公</div><div class="line">class Student&#123;</div><div class="line">	// 私有的静态属性保存当前实例</div><div class="line">	private static $instance;</div><div class="line">	// 私有的构造函数阻止在类的外部实例化</div><div class="line">	private function __construnct()&#123;&#125;</div><div class="line">	// 私有的__clone() 用来阻止在类外部clone对象</div><div class="line">	private function __clone()&#123;&#125;</div><div class="line">	// 公有的静态方法用来获取实例</div><div class="line">	public static function getInstance()&#123;</div><div class="line">		if (!self::$instance instanceof self) &#123;</div><div class="line">			self::$instance = new self;</div><div class="line">		&#125;</div><div class="line">		return self::$instance;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">//单例模式</div><div class="line">$stu1 = Student::getInstance();</div><div class="line">$stu2 = Student::getInstance();</div><div class="line"></div><div class="line">var_dump($stu1);</div><div class="line">echo &quot;&lt;br&gt;&quot;;</div><div class="line">var_dump($stu2);</div><div class="line">?&gt;</div><div class="line">object(Student)#1 (0) &#123; &#125; </div><div class="line">object(Student)#1 (0) &#123; &#125;</div></pre></td></tr></table></figure>
<blockquote>
<p>instanceof用来判断变量是否属于某种数据类型</p>
</blockquote>
<h4 id="项目层面的单例模式（一个项目中有一组单例模式）"><a href="#项目层面的单例模式（一个项目中有一组单例模式）" class="headerlink" title="项目层面的单例模式（一个项目中有一组单例模式）"></a>项目层面的单例模式（一个项目中有一组单例模式）</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">class DB1 &#123;</div><div class="line">	private static $instance;</div><div class="line">	private function __construct() &#123;</div><div class="line">	&#125;</div><div class="line">	private function __clone() &#123;</div><div class="line">	&#125;</div><div class="line">	public static function getInstance() &#123;</div><div class="line">		if(!self::$instance instanceof self)	//实例的数据类型不是Student类型的</div><div class="line"> 			self::$instance=new self;</div><div class="line"> 		return self::$instance;</div><div class="line"> 	&#125;</div><div class="line"> &#125;</div><div class="line">class DB2 &#123;</div><div class="line">	private static $instance;</div><div class="line">	private function __construct() &#123;</div><div class="line">	&#125;</div><div class="line">	private function __clone() &#123;</div><div class="line">	&#125;</div><div class="line">	public static function getInstance() &#123;</div><div class="line">		if(!self::$instance instanceof self)	//实例的数据类型不是Student类型的</div><div class="line">			self::$instance=new self;</div><div class="line">		return self::$instance;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">class DB3 &#123;</div><div class="line">	private static $instance;</div><div class="line">	private function __construct() &#123;</div><div class="line">	&#125;</div><div class="line">	private function __clone() &#123;</div><div class="line">	&#125;</div><div class="line">	public static function getInstance() &#123;</div><div class="line">		if(!self::$instance instanceof self)	//实例的数据类型不是Student类型的</div><div class="line">			self::$instance=new self;</div><div class="line">		return self::$instance;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">//项目层面的单例模式</div><div class="line">function getInstance($db_name) &#123;</div><div class="line">	static $array=array();</div><div class="line">	if(!isset($array[$db_name]))</div><div class="line">		$array[$db_name]=$db_name::getInstance($db_name);</div><div class="line">	return $array[$db_name];</div><div class="line">&#125;</div><div class="line">//测试</div><div class="line">$db1=getInstance(&apos;DB1&apos;);</div><div class="line">$db2=DB1::getInstance();</div><div class="line">var_dump($db1,$db2);</div><div class="line">?&gt;</div></pre></td></tr></table></figure>
<blockquote>
<p>object(DB1)#1 (0) { } object(DB1)#1 (0) { }</p>
</blockquote>
<h3 id="工厂模式"><a href="#工厂模式" class="headerlink" title="工厂模式"></a>工厂模式</h3><p>传递不同的参数获取不同的对象</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class ProductA &#123;</div><div class="line">	public function getProduct() &#123;</div><div class="line">		echo &apos;这是A商品&lt;br&gt;&apos;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">class ProductB&#123;</div><div class="line">	public function getProduct() &#123;</div><div class="line">		echo &apos;这是B商品&lt;br&gt;&apos;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">//工厂模式</div><div class="line">class ProductFactory &#123;</div><div class="line">	public static function create($num) &#123;</div><div class="line">		switch($num) &#123;</div><div class="line">			case 1:</div><div class="line">				return new ProductA();</div><div class="line">			case 2:</div><div class="line">				return new ProductB;</div><div class="line">		&#125;</div><div class="line">		return null;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">$objA=ProductFactory::create(1);</div><div class="line">$objB=ProductFactory::create(2);</div><div class="line">$objA-&gt;getProduct();</div><div class="line">$objB-&gt;getProduct();</div><div class="line"> ?&gt;</div><div class="line">这是A商品</div><div class="line">这是B商品</div></pre></td></tr></table></figure>
<h3 id="策略模式"><a href="#策略模式" class="headerlink" title="策略模式"></a>策略模式</h3><p>传递不同的参数，调用不同的方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">//策略模式</div><div class="line">interface IStrategy &#123;</div><div class="line">	function ontheway();</div><div class="line">&#125;</div><div class="line"></div><div class="line">class Walk implements IStrategy &#123;</div><div class="line">	public function ontheway() &#123;</div><div class="line">		echo &apos;走着去&lt;br&gt;&apos;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">class Bike implements IStrategy&#123;</div><div class="line">	public function ontheway() &#123;</div><div class="line">		echo &apos;骑车去&lt;br&gt;&apos;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">class Bus implements IStrategy &#123;</div><div class="line">	public function ontheway() &#123;</div><div class="line">		echo &apos;坐巴士去&lt;br&gt;&apos;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">//传递不同的参数，调用不同方法</div><div class="line">class Strategy &#123;</div><div class="line">	public function getWay(IStrategy $obj) &#123;</div><div class="line">		$obj-&gt;ontheway();</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">$obj=new Strategy();</div><div class="line">$obj-&gt;getWay(new Walk);</div><div class="line">$obj-&gt;getWay(new Bike);</div><div class="line">$obj-&gt;getWay(new Bus);</div><div class="line"> ?&gt;</div><div class="line">走着去</div><div class="line">骑车去</div><div class="line">坐巴士去</div></pre></td></tr></table></figure>
<h3 id="魔术方法"><a href="#魔术方法" class="headerlink" title="魔术方法"></a>魔术方法</h3><h4 id="tostring"><a href="#tostring" class="headerlink" title="__tostring()"></a>__tostring()</h4><p>当把对象当成字符串调用时自动执行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Student&#123;</div><div class="line">	//当把对象当成字符串调用时自动执行</div><div class="line">	public function __toString()&#123;</div><div class="line">		return &quot;这是一个学生对象&quot;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$stu=new Student;</div><div class="line">echo $stu;</div><div class="line"> ?&gt;</div><div class="line">这是一个学生对象</div></pre></td></tr></table></figure>
<h4 id="invoke"><a href="#invoke" class="headerlink" title="__invoke()"></a>__invoke()</h4><p>当把对象当成函数调用的时候自动执行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Student&#123;</div><div class="line">	//当把对象当成函数调用的时候自动执行</div><div class="line">	public function __invoke($args)&#123;</div><div class="line">		echo &quot;这是一个学生对象，你传递的参数是：&quot;.$args;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">$stu = new Student();</div><div class="line">$stu(&apos;tom&apos;)</div><div class="line"> ?&gt;</div><div class="line">这是一个学生对象，你传递的参数是：tom</div></pre></td></tr></table></figure>
<h4 id="set-、-get"><a href="#set-、-get" class="headerlink" title="__set()、__get()"></a><code>__set()、__get()</code></h4><p><strong>set()：当给无法访问的属性<em>赋值</em>的时候自动调用
</strong>get()：当<em>获取</em>无法访问的属性值的时候自动调用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Student&#123;</div><div class="line">	private $name;</div><div class="line">	// 当给无法访问的属性赋值时自动调用</div><div class="line">	public function __set($key,$value)&#123;</div><div class="line">		$this-&gt;$key=$value;</div><div class="line">	&#125;</div><div class="line">	//当获取无法访问的属性值的时候自动调用</div><div class="line">	public function __get($key)&#123;</div><div class="line">		return $this-&gt;$key;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$stu = new Student();</div><div class="line">$stu-&gt;name=&apos;tom&apos;;</div><div class="line">echo $stu-&gt;name;</div><div class="line"> ?&gt;</div><div class="line">tom</div></pre></td></tr></table></figure>
<h4 id="unset-、-isset"><a href="#unset-、-isset" class="headerlink" title="__unset()、__isset()"></a><code>__unset()、__isset()</code></h4><p><strong>unset():当销毁无法访问的属性的时候自动调用
</strong>isset():当判断无法访问的属性是否存在时自动调用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Student&#123;</div><div class="line">	private $name;</div><div class="line">	private $sex;</div><div class="line">	private $array=array(&apos;name&apos;,&apos;sex&apos;);</div><div class="line">	// 当销毁无法访问的属性的时候自动调用</div><div class="line">	public function __unset($key)&#123;</div><div class="line">		unset($this-&gt;$key);</div><div class="line">	&#125;</div><div class="line">	// 当判断无法访问的属性时自动调用</div><div class="line">	public function __isset($key)&#123;</div><div class="line">		return in_array($key,$this-&gt;array);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$stu=new Student();</div><div class="line">echo isset($stu-&gt;name)?&apos;yes&apos;:&apos;no&apos;;</div><div class="line">echo &quot;&lt;br&gt;&quot;;</div><div class="line">echo isset($stu-&gt;sss)?&apos;yes&apos;:&apos;no&apos;;</div><div class="line"> ?&gt;</div><div class="line">yes</div><div class="line">no</div></pre></td></tr></table></figure>
<h4 id="call-、-callstatic"><a href="#call-、-callstatic" class="headerlink" title="__call()、__callstatic()"></a><code>__call()、__callstatic()</code></h4><p><strong>call():当调用无法访问的普通方法时候自动调用
</strong>callstatic():当调用无法访问的静态方法时候自动调用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Student&#123;</div><div class="line">	/**</div><div class="line">	 * 当调用无法访问的方法时自动调用</div><div class="line">	 * @param $fn_name string 传递方法名</div><div class="line">	 * @param $fn_args array 传递参数数组</div><div class="line">	 */</div><div class="line">	public function __call($fn_name,$fn_args)&#123;</div><div class="line">		echo &quot;不能调用&quot;.$fn_name.&quot;方法&lt;br&gt;&quot;;</div><div class="line">		var_dump($fn_args);</div><div class="line">		//echo implode(&apos;,&apos;,$fn_args);</div><div class="line">	&#125;</div><div class="line">	// 当输出静态方法调用一个无法访问的方式时自动调用</div><div class="line">	public static function __callStatic($fn_name,$fn_args)&#123;</div><div class="line">		echo &quot;&lt;br&gt;不能调用静态&quot;.$fn_name.&apos;方法&lt;br&gt;&apos;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$stu = new Student();</div><div class="line">$stu-&gt;show(10,20);</div><div class="line">Student::test();</div><div class="line"> ?&gt;</div><div class="line">不能调用show方法</div><div class="line">array(2) &#123; [0]=&gt; int(10) [1]=&gt; int(20) &#125; </div><div class="line">不能调用静态test方法</div></pre></td></tr></table></figure>
<h3 id="Iterator（迭代器）"><a href="#Iterator（迭代器）" class="headerlink" title="Iterator（迭代器）"></a>Iterator（迭代器）</h3><p>Iterator是迭代器，是PHP内置的接口，用来遍历类中的某个属性。迭代器中有5个抽象方法</p>
<ol>
<li>Iterator::current     -返回当前元素</li>
<li>Iterator::key        -返回当前元素的键</li>
<li>Iterator::next        -向前移动到下一个元素</li>
<li>Iterator::rewind        -返回到迭代器的第一个元素</li>
<li>Iterator::valid        -检查当前位置是否有效</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">//学生类</div><div class="line">class Student &#123;</div><div class="line">private $name;</div><div class="line">public function __construct($name) &#123;</div><div class="line">	$this-&gt;name=$name;</div><div class="line">&#125;</div><div class="line">public function __get($key) &#123;</div><div class="line">	return $this-&gt;$key;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">//班级类</div><div class="line">class MyClass implements Iterator &#123;</div><div class="line">	private $stu_list=array();	//学生数组</div><div class="line">	public function addStu(Student $stu) &#123;</div><div class="line">		$this-&gt;stu_list[]=$stu;</div><div class="line">	&#125;</div><div class="line">	//指针复位到数组的第一个元素</div><div class="line">	public function rewind() &#123;</div><div class="line">		reset($this-&gt;stu_list);</div><div class="line">	&#125;</div><div class="line">	//检查当前指针的位置是否合法</div><div class="line">	public function valid() &#123;</div><div class="line">		return key($this-&gt;stu_list)!==null;</div><div class="line">	&#125;</div><div class="line">	//返回当前值</div><div class="line">	public function current() &#123;</div><div class="line">		return current($this-&gt;stu_list);</div><div class="line">	&#125;</div><div class="line">	//返回当前键</div><div class="line">	public function key() &#123;</div><div class="line">		return key($this-&gt;stu_list);</div><div class="line">	&#125;</div><div class="line">	//指针下移一位</div><div class="line">	public function next() &#123;</div><div class="line">		next($this-&gt;stu_list);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">//创建班级</div><div class="line">$class=new MyClass;</div><div class="line">$class-&gt;addStu(new Student(&apos;tom&apos;));</div><div class="line">$class-&gt;addStu(new Student(&apos;berry&apos;));</div><div class="line">$class-&gt;addStu(new Student(&apos;Ketty&apos;));</div><div class="line">//遍历班级，就是遍历学生数组</div><div class="line">foreach($class as $stu) &#123;</div><div class="line">	echo $stu-&gt;name,&apos;&lt;br&gt;&apos;;</div><div class="line">&#125;</div><div class="line"> ?&gt;</div><div class="line">tom</div><div class="line">berry</div><div class="line">Ketty</div></pre></td></tr></table></figure>
<h3 id="数组的序列化（serialize）和反序列化（unserialize）"><a href="#数组的序列化（serialize）和反序列化（unserialize）" class="headerlink" title="数组的序列化（serialize）和反序列化（unserialize）"></a>数组的序列化（serialize）和反序列化（unserialize）</h3><h3 id="对象的序列化和反序列化"><a href="#对象的序列化和反序列化" class="headerlink" title="对象的序列化和反序列化"></a>对象的序列化和反序列化</h3>
          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/20160915-1.html" itemprop="url">
                  Day2-PHP面向对象
                </a>
              
            
          </h1>
        

        <div class="post-meta">
        
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-09-15T21:40:36+08:00" content="2016-09-15">
              2016-09-15
            </time>
          </span>

          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/20160915-1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="20160915-1.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="多态"><a href="#多态" class="headerlink" title="多态"></a>多态</h3><p>多态就是多种形态<br>多态分为：方法重载和方法重写，但是PHP不支持方法重载。</p>
<h4 id="方法重写"><a href="#方法重写" class="headerlink" title="方法重写"></a>方法重写</h4><p>子类方法和父类的方法必须同名</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">animal</span></span>&#123;</div><div class="line">	<span class="keyword">public</span> $name=<span class="string">'动物'</span>;</div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">jiao</span><span class="params">()</span></span>&#123;</div><div class="line">		<span class="keyword">echo</span> <span class="keyword">$this</span>-&gt;name.<span class="string">"会叫&lt;br&gt;"</span>;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">dog</span> <span class="keyword">extends</span> <span class="title">animal</span></span>&#123;</div><div class="line">	<span class="keyword">public</span> $name=<span class="string">'狗'</span>;</div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">jiao</span><span class="params">()</span></span>&#123;</div><div class="line">		<span class="keyword">echo</span> <span class="keyword">$this</span>-&gt;name.<span class="string">"会叫&lt;br&gt;"</span>;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$dog = <span class="keyword">new</span> dog();</div><div class="line">$dog-&gt;jiao();</div><div class="line"></div><div class="line">  <span class="meta">?&gt;</span></div><div class="line">var_dump($obj);</div><div class="line">object(dog)<span class="comment">#1 (1) &#123; ["name"]=&gt; string(3) "狗" &#125;</span></div></pre></td></tr></table></figure>
<blockquote>
<p>1.子类重写的方法不能比父类的方法更加严格(封装)<br>public、protected、private<br>2.子类可以拥有和父类不同个数的参数，[严格标准是不允许的]</p>
</blockquote>
<h4 id="私有属性的继承和重写"><a href="#私有属性的继承和重写" class="headerlink" title="私有属性的继承和重写"></a>私有属性的继承和重写</h4><p>私有属性可以被继承但不能被重写</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class animal&#123;</div><div class="line">	private $name=&apos;动物&apos;;</div><div class="line">	public function jiao()&#123;</div><div class="line">		echo $this-&gt;name.&quot;会叫&lt;br&gt;&quot;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class dog extends animal&#123;</div><div class="line">	private $name=&apos;狗&apos;;</div><div class="line">	public function jiao()&#123;</div><div class="line">		echo $this-&gt;name.&quot;会叫&lt;br&gt;&quot;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$dog = new dog();</div><div class="line">var_dump($dog);</div><div class="line">?&gt;</div><div class="line">object(dog)#1 (2) &#123; [&quot;name&quot;:&quot;dog&quot;:private]=&gt; string(3) &quot;狗&quot; [&quot;name&quot;:&quot;animal&quot;:private]=&gt; string(6) &quot;动物&quot; &#125;</div></pre></td></tr></table></figure>
<h3 id="访问修饰符"><a href="#访问修饰符" class="headerlink" title="访问修饰符"></a>访问修饰符</h3><p>static        静态的<br>final        最终的<br>abstract    抽象的</p>
<h4 id="static【静态的】"><a href="#static【静态的】" class="headerlink" title="static【静态的】"></a>static【静态的】</h4><p>1、    static修饰属性叫静态属性，static修饰方法叫静态方法<br>2、    当类加载的时候就分配内存空间，在内存中就一份。不用实例化可以直接访问。为所有的对象共享。<br>3、    销毁对象不会销毁静态成员，因为静态成员属于类，而不属于对象。<br>4、    当页面执行完毕的时候才销毁静态成员。<br>5、    父类的static属性子类中可以继承并重写，但不能重新定义同名普通属性。<br>6、    静态方法中不能直接访问普通属性<br>7、    被self调用的普通方法会转成静态方法，【严格标准不允许】</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Person&#123;</div><div class="line">	public static $name = &apos;人类&apos;;</div><div class="line">	public static function show()&#123;</div><div class="line">		// echo &apos;这个星球的主宰者是：&apos;.$this-&gt;name;(报错：Fatal error: Using $this when not in object context in)</div><div class="line">		echo &apos;这个星球的主宰者是：&apos;.Person::$name;//(正确)</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">echo Person::$name,&quot;&lt;br&gt;&quot;;</div><div class="line">Person::show();</div><div class="line"> ?&gt;</div></pre></td></tr></table></figure>
<p><strong>静态成员可以被继承</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Person&#123;</div><div class="line">	public static $name = &apos;人类&apos;;</div><div class="line">	public static function show()&#123;</div><div class="line">		// echo &apos;这个星球的主宰者是：&apos;.$this-&gt;name;(报错：Fatal error: Using $this when not in object context in)</div><div class="line">		echo &apos;这个星球的主宰者是：&apos;.Person::$name;//(正确)</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class Student extends Person&#123;</div><div class="line"></div><div class="line">&#125;</div><div class="line">echo Person::$name,&quot;&lt;br&gt;&quot;;</div><div class="line">Person::show();</div><div class="line">echo &quot;&lt;br&gt;&quot;;</div><div class="line">echo Student::$name,&apos;&lt;br&gt;&apos;;</div><div class="line">Student::show();</div><div class="line">echo &apos;&lt;br&gt;&apos;;</div><div class="line"> ?&gt;</div></pre></td></tr></table></figure>
<blockquote>
<p>调用静态成员：类名::静态成员</p>
<p>self 表示当前类名<br>非静态方法被self调用，自动将非静态方法转成静态方法【严格标准是不允许的】</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Teacher&#123;</div><div class="line">	public function show()&#123;</div><div class="line">		echo &quot;stay hungry&quot;;</div><div class="line">	&#125;</div><div class="line">	public function test()&#123;</div><div class="line">		$obj = new self();</div><div class="line">		$obj-&gt;show();</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">Teacher::test();</div><div class="line"></div><div class="line"> ?&gt;</div></pre></td></tr></table></figure>
<h4 id="确定当前对象"><a href="#确定当前对象" class="headerlink" title="确定当前对象"></a>确定当前对象</h4><p>1、$this表示当前对象的引用<br>2、self表示当前方法所在的类的类名<br>3、static表示当前对象所属的类的类名【静态延时绑定】<br>    最后一次执行类的属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">class Person &#123;</div><div class="line">	public static $type=&apos;人类&apos;;</div><div class="line">	public function showPerson() &#123;</div><div class="line">		//var_dump($this);	//object(Student)#1 (0) &#123; &#125;</div><div class="line">		//echo self::$type;	//人类</div><div class="line">		echo static::$type;	//学生    静态延时绑定,表示Student</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">class Student extends Person &#123;</div><div class="line">	public static $type=&apos;学生&apos;;</div><div class="line">	public function showStu() &#123;</div><div class="line">		//var_dump($this);	//object(Student)#1 (0) &#123; &#125;</div><div class="line">		//echo self::$type;	//学生</div><div class="line">		echo static::$type;	//学生</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">//测试</div><div class="line">header(&apos;content-type:text/html;charset=utf-8&apos;);</div><div class="line">$obj=new Student;</div><div class="line">$obj-&gt;showPerson();</div><div class="line">$obj-&gt;showStu();</div></pre></td></tr></table></figure>
<h4 id="final-最终的"><a href="#final-最终的" class="headerlink" title="final[最终的]"></a>final[最终的]</h4><p>1、    final修饰的类不能被继承<br>2、    final修饰的方法不能被重写<br>3、    final不能修饰属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">final class animal&#123;</div><div class="line">	public $name=&apos;动物&apos;;</div><div class="line">	public function jiao()&#123;</div><div class="line">		echo $this-&gt;name.&quot;会叫&lt;br&gt;&quot;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class dog extends animal&#123;</div><div class="line">	public $name=&apos;狗&apos;;</div><div class="line">	public function jiao()&#123;</div><div class="line">		echo $this-&gt;name.&quot;会叫&lt;br&gt;&quot;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$dog = new dog();</div><div class="line">echo $dog-&gt;name,&quot;&lt;br&gt;&quot;;</div><div class="line">$dog-&gt;jiao();</div><div class="line"> ?&gt;</div><div class="line"></div><div class="line">Fatal error: Class dog may not inherit from final class (animal) in</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class animal&#123;</div><div class="line">	public $name=&apos;动物&apos;;</div><div class="line">	final public function jiao()&#123;</div><div class="line">		echo $this-&gt;name.&quot;会叫&lt;br&gt;&quot;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class dog extends animal&#123;</div><div class="line">	public $name=&apos;狗&apos;;</div><div class="line">	public function jiao()&#123;</div><div class="line">		echo $this-&gt;name.&quot;会叫&lt;br&gt;&quot;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$dog = new dog();</div><div class="line">echo $dog-&gt;name,&quot;&lt;br&gt;&quot;;</div><div class="line">$dog-&gt;jiao();</div><div class="line"> ?&gt;</div><div class="line">Fatal error: Cannot override final method animal::jiao() in</div></pre></td></tr></table></figure>
<h4 id="abstract-抽象的"><a href="#abstract-抽象的" class="headerlink" title="abstract[抽象的]"></a>abstract[抽象的]</h4><p>1、    abstract修饰的类成为抽象类，abstract修饰的方法叫抽象方法<br>2、    抽象方法：只有方法的声明，没有方法的实现<br>3、    抽象类：类中只要有一个方法是抽象方法，这个类就是抽象类。<br>4、    抽象类不可以被实例化，必须在子类中重新实现。</p>
<blockquote>
<p>抽象类的作用：定义方法的命名规范</p>
</blockquote>
<h3 id="接口（interface）"><a href="#接口（interface）" class="headerlink" title="接口（interface）"></a>接口（interface）</h3><p>1、    如果一个类中所有的方法都是抽象方法，这个类就声明成接口。<br>2、    接口是一个特殊的抽象类<br>3、    接口中的方法只能是public的，默认是public的<br>4、    接口中的方法不能用abstract、final来修饰。<br>5、    定义过程用<code>interface</code>实现接口用的<code>implements</code>关键字</p>
<blockquote>
<p>类不能多重继承，但是接口可以多重实现</p>
</blockquote>
<h4 id="继承类的同时实现接口"><a href="#继承类的同时实现接口" class="headerlink" title="继承类的同时实现接口"></a>继承类的同时实现接口</h4><p>必须先继承类，再实现接口</p>
<p><code>class Myclass extends Class implements IPict1,IPict2</code></p>
<h3 id="类常量【const】"><a href="#类常量【const】" class="headerlink" title="类常量【const】"></a>类常量【const】</h3><p>类中可以放属性、方法、常量<br>调用类常量    类名::常量名<br>类常量属于类自身，不属于对象实例，不能通过对象实例访问<br>子类可以重写父类中的常量，可以通过(parent::)来调用父类中的常量</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Student &#123;</div><div class="line">	const type=&apos;学生&apos;;	//类常量</div><div class="line">	public function show() &#123;</div><div class="line">		echo self::type,&apos;&lt;br&gt;&apos;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">//测试</div><div class="line">$stu=new Student;</div><div class="line">$stu-&gt;show();	//学生</div><div class="line"> ?&gt;</div></pre></td></tr></table></figure>
<blockquote>
<p>类常量也可以放在接口中</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">interface Person&#123;</div><div class="line">	const type=&apos;人类&apos;;</div><div class="line">&#125;</div><div class="line">echo Person::type;</div><div class="line"> ?&gt;</div></pre></td></tr></table></figure>
<h3 id="参数约束"><a href="#参数约束" class="headerlink" title="参数约束"></a>参数约束</h3><p>1、    在PHP5.3以后才支持参数约束<br>2、    只能约束对象，不能约束基本数据类型</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Student&#123;</div><div class="line">&#125;</div><div class="line">function show(Student $obj)&#123;</div><div class="line">	var_dump($obj);</div><div class="line">&#125;</div><div class="line">show(10);</div><div class="line"></div><div class="line">?&gt;</div><div class="line">Catchable fatal error: Argument 1 passed to show() must be an instance of Student, integer given, called in /Users/uiste/www/test.php on line 95 and defined in /Users/uiste/www/test.php on line 92</div><div class="line">只能传递Student类型的数据</div><div class="line"></div><div class="line">&lt;?php </div><div class="line">class Student&#123;</div><div class="line">&#125;</div><div class="line">function show(Student $obj)&#123;</div><div class="line">	var_dump($obj);</div><div class="line">&#125;</div><div class="line">show(new Student);</div><div class="line"> ?&gt;</div><div class="line">object(Student)#1 (0) &#123; &#125;</div></pre></td></tr></table></figure>
<h3 id="父类可以指向子类的引用"><a href="#父类可以指向子类的引用" class="headerlink" title="父类可以指向子类的引用"></a>父类可以指向子类的引用</h3><p>父类对象可以保存子类的地址</p>
<p>面向对象的三大特性：封装、继承、多态</p>
<h3 id="类的自动加载"><a href="#类的自动加载" class="headerlink" title="类的自动加载"></a>类的自动加载</h3><p>1、    一个文件中只能有一个类<br>2、    类文件名以.class.php结尾<br>3、    类名和文件名同名</p>
<p>Goods.class.php<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">/***商品类*/</div><div class="line">abstract class Goods &#123;</div><div class="line">	protected $name;</div><div class="line">	//设置商品名称</div><div class="line">	public function setName($name) &#123;</div><div class="line">		$this-&gt;name=$name;</div><div class="line">	&#125;</div><div class="line">	//获取商品名称</div><div class="line">	abstract function getName();</div><div class="line">&#125;</div><div class="line">?&gt;</div></pre></td></tr></table></figure></p>
<p>Books.class.php<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">class Books extends Goods &#123;</div><div class="line">	public function getName() &#123;</div><div class="line">		echo &quot;《&#123;$this-&gt;name&#125;》&quot;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">?&gt;</div></pre></td></tr></table></figure></p>
<p>Phone.class.php<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">class Phone extends Goods &#123;</div><div class="line">	public function getName() &#123;</div><div class="line">		echo $this-&gt;name;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">?&gt;</div></pre></td></tr></table></figure></p>
<p>loadClass.php<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">header(&apos;content-type:text/html;charset=utf-8&apos;);</div><div class="line">require &apos;./Goods.class.php&apos;;</div><div class="line">require &apos;./Books.class.php&apos;;</div><div class="line">require &apos;./Phone.class.php&apos;;</div><div class="line">$book=new 45Books();</div><div class="line">$phone=new Phone();</div><div class="line">$book-&gt;setName(&apos;PHP高级&apos;);</div><div class="line">$book-&gt;getName();</div><div class="line">?&gt;</div></pre></td></tr></table></figure></p>
<p>手动引入类比较麻烦，不健壮。最好能实现需要什么类就自动加载什么类。</p>
<p>####　<code>__autoload($class_name)</code><br>当页面执行的时候，PHP核心程序（Zend Engine）判断当前需要哪个类，如果没有找到该类，就自动的调用<code>__autoload()</code>函数，并且将缺少的类名作为参数传递到<code>__autoload()</code>函数中。</p>
<h4 id="通过缺少类名来加载需要的类"><a href="#通过缺少类名来加载需要的类" class="headerlink" title="通过缺少类名来加载需要的类"></a>通过缺少类名来加载需要的类</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">$book = new Books();</div><div class="line">$book-&gt;setname(&quot;PHP高级&quot;);</div><div class="line">$book-&gt;getname();</div><div class="line"></div><div class="line">function __autoload($class_name)&#123;</div><div class="line">	require &quot;./$class_name.class.php&quot;;</div><div class="line">&#125;</div><div class="line"> ?&gt;</div></pre></td></tr></table></figure>
<h4 id="将类映射到数组中"><a href="#将类映射到数组中" class="headerlink" title="将类映射到数组中"></a>将类映射到数组中</h4><p>有的时候类存放的位置不规则，将不规则的地址映射到类名，形成一个数组再加载。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">function __autoload($class_name)&#123;</div><div class="line">	$map = array(</div><div class="line">		&apos;Goods&apos; =&gt; &apos;./aa/Goods.class.php&apos;,</div><div class="line">		&apos;Books&apos; =&gt; &apos;./bb/Books.class.php&apos;,</div><div class="line">		&apos;Phone&apos; =&gt; &apos;./cc/Phone.class.php&apos;</div><div class="line">	);</div><div class="line">	if (isset($map[$class_name])) &#123;</div><div class="line">		require $map[$class_name];</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"> ?&gt;</div></pre></td></tr></table></figure>
<h4 id="利用命名规则加载类"><a href="#利用命名规则加载类" class="headerlink" title="利用命名规则加载类"></a>利用命名规则加载类</h4><p>同一类别的类放在同一个文件夹下，这样便于分组管理</p>
<p>将所有的类文件放到Lib文件夹下。将类名名字改为：类名+文件夹名</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">1.常规引入方法</div><div class="line"> require &apos;Goods.class.php&apos;;</div><div class="line"> require &apos;Book.class.php&apos;;</div><div class="line">2.通过调用autoload自动加载类名，加载缺少类名的类</div><div class="line"> function __autoload($class_name)&#123;</div><div class="line"> 	require &quot;./&#123;$class_name&#125;.class.php&quot;;</div><div class="line"> &#125;</div><div class="line"> 3.将类名与地址映射到关联数组中去加载</div><div class="line"> function __autoload($class_name)&#123;</div><div class="line"> 	$arr = array(</div><div class="line"> 		&apos;Book&apos;=&gt;&apos;Book.class.php&apos;,</div><div class="line"> 		&apos;Phone&apos;=&gt;&apos;Phone.class.php&apos;,</div><div class="line"> 		&apos;Goods&apos;=&gt;&apos;Goods.class.php&apos;</div><div class="line"> 	);</div><div class="line"> 	if (isset($arr[$class_name]))</div><div class="line"> 		require &quot;./&#123;$arr[$class_name]&#125;&quot;;</div><div class="line"> &#125;</div><div class="line"> 4.利用命名规则加载类一个项目中会有很多类，我们会将类分类存放，类的命令规则：在Lib文件夹下的类以Lib结尾，在Model文件夹的类以Model结尾。</div><div class="line"> function __autoload($class_name)&#123;</div><div class="line"> 	if (substr($class_name,-3)==&apos;Lib&apos;)</div><div class="line"> 		require &quot;./Lib/&#123;$class_name&#125;.class.php&quot;;</div><div class="line"> 	elseif (substr($class_name,-3)==&apos;Control&apos;)</div><div class="line"> 		require &quot;./Control/&#123;$class_name&#125;.class.php&quot;;</div><div class="line"> 	elseif (substr($class_name,-3)==&apos;Model&apos;)</div><div class="line"> 		require &quot;./Model/&#123;$class_name&#125;.class.php&quot;;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<blockquote>
<p>Final 和 abstract 不能修饰接口中的方法<br>参数约束只能约束对象</p>
</blockquote>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/20160915-1.html" itemprop="url">
                  Day1-PHP面向对象
                </a>
              
            
          </h1>
        

        <div class="post-meta">
        
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-09-15T21:40:18+08:00" content="2016-09-15">
              2016-09-15
            </time>
          </span>

          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/20160915-1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="20160915-1.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p>1、    OOP：Object Oriented Programming    面向对象编程<br>2、    OOA：Object-Oriented Analysis    面向对象分析<br>3、    OOD：Object-Oriented Design        面向对象设计<br>4、    对象只能有属性、方法、和常量</p>
<blockquote>
<p>类-数据类型<br>对象-复杂的变量<br>属性-变量<br>function-方法、函数</p>
</blockquote>
<h3 id="类"><a href="#类" class="headerlink" title="类"></a>类</h3><p>类名不区分大小写</p>
<p>通过new关键字类实例化对象，实例化的过程就是分配内存的过程<br><code>$stu = new Student();//实例化对象就是把对象的地址传递给$stu1变量</code> </p>
<h3 id="封装"><a href="#封装" class="headerlink" title="封装"></a>封装</h3><p>a)    public：公有的            可以在类的内部和外部访问<br>b)    protected：受保护的        在整个继承链上访问<br>c)    private：私有的            只能在类的内部访问</p>
<blockquote>
<p>属性一般是私有的，通过公有的方法对私有的属性进行赋值和取值。这样做为了保证数据完整性<br>$this在类的内部使用，表示调用当前方法的对象</p>
</blockquote>
<h3 id="类、对象、属性、方法在内存中的表现"><a href="#类、对象、属性、方法在内存中的表现" class="headerlink" title="类、对象、属性、方法在内存中的表现"></a>类、对象、属性、方法在内存中的表现</h3><p>内存区域<br>静态区和常量区<br>代码区    &lt;===&gt;    类<br>堆区        &lt;===&gt;    对象<br>栈区        &lt;===&gt;    对象的变量，变量指向的是对象的地址</p>
<blockquote>
<p>调用属性直接调用对象中的对应属性即可。<br>调用方法先获取对象所在的类，然后调用类中的方法。</p>
</blockquote>
<p>类的本质是一个复杂的数据类型；对象的本质是内存中的空间，是一个复杂的变量</p>
<h3 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h3><p><code>__construct()</code><br>构造函数：在对象产生的时候自动调用<br>a)    可以传递不同的参数<br>b)    用来初始化成员变量（属性）</p>
<blockquote>
<p>跟类名同名的方法也是构造方法;PHP中推荐使用__construct()做构造函数,且优先级较高</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Student &#123;</div><div class="line">	//构造函数语法二</div><div class="line">	public function Student() &#123;</div><div class="line">		echo &apos;i am a boy!&lt;br&gt;&apos;;</div><div class="line">	&#125;</div><div class="line">	//构造函数语法一(优先级高)</div><div class="line">	public function __construct() &#123;</div><div class="line">		echo &apos;i am a student&lt;br&gt;&apos;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">//测试</div><div class="line">new Student;	//i am a student</div><div class="line">new Student;	//i am a student</div><div class="line"> ?&gt;</div></pre></td></tr></table></figure>
<h3 id="析构函数"><a href="#析构函数" class="headerlink" title="析构函数"></a>析构函数</h3><p><code>__destruct()</code><br>析构函数：在对象销毁的时候自动调用<br>a)    析构函数不能有参数</p>
<h3 id="继承（extends）"><a href="#继承（extends）" class="headerlink" title="继承（extends）"></a>继承（extends）</h3><p>语法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">Class 子类 extends 父类&#123;</div><div class="line">	//类成员</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="私有的属性也是可以被继承的"><a href="#私有的属性也是可以被继承的" class="headerlink" title="私有的属性也是可以被继承的"></a>私有的属性也是可以被继承的</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">class Person&#123;</div><div class="line">	private $name = &apos;uiste&apos;;</div><div class="line">	public function show()&#123;</div><div class="line">		echo &quot;好学生就是：&quot;.$this-&gt;name;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class Student extends Person&#123;</div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line">$stu1 = new Student();</div><div class="line">$stu1-&gt;show();</div><div class="line">?&gt;</div><div class="line">好学生就是：uiste</div></pre></td></tr></table></figure>
<blockquote>
<p>如果父类private私有属性重写的时候，无论子类属性是：public、protected、private都会生成性的属性;<br>如果父类是protected受把偶的属性时，子类不可以是private私有的。只能是protected、public，且属性会被重写；<br>如果父类是public公有的属性时，子类也只能是public公有的；</p>
</blockquote>
<p>总之：父类属性不是私有状态，子类的属性就不能比父类属性的权限严格。<br>父类属性时私有状态是，子类的属性可以任何状态，重写则会被重新建立。</p>
<h4 id="子类中调用父类的属性和方法"><a href="#子类中调用父类的属性和方法" class="headerlink" title="子类中调用父类的属性和方法"></a>子类中调用父类的属性和方法</h4><p>调用父类的属性和方法用$this关键字</p>
<h4 id="调用父类的构造方法"><a href="#调用父类的构造方法" class="headerlink" title="调用父类的构造方法"></a>调用父类的构造方法</h4><p>通过parent和父类的类名来调用父类的构造函数<br>语法：<br><code>parent::__construct()</code><br><code>父类类名::__construct()</code></p>
<blockquote>
<p>如果子类有构造函数就调用子类的构造函数，如果子类没有就调用父类的构造函数</p>
</blockquote>
<p>1、    继承：extends关键字<br>2、    $this：当前对象的引用<br>3、    parent：父类的类名<br>4、    私有属性也是可以被继承的</p>
<h4 id="给父类的构造函数传递参数"><a href="#给父类的构造函数传递参数" class="headerlink" title="给父类的构造函数传递参数"></a>给父类的构造函数传递参数</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">&lt;?php </div><div class="line">class Person&#123;</div><div class="line">	private $name;</div><div class="line">	private $sex;</div><div class="line">	public function __construct($name,$sex)&#123;</div><div class="line">		$this-&gt;name = $name;</div><div class="line">		$this-&gt;sex = $sex;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class Student extends Person&#123;</div><div class="line">	private $score;</div><div class="line">	public function __construct($name,$sex,$score)&#123;</div><div class="line">		parent::__construct($name,$sex);</div><div class="line">		$this-&gt;score = $score;</div><div class="line">	&#125;</div><div class="line">	public function show()&#123;</div><div class="line">		echo $this-&gt;name.&apos;是&apos;.$this-&gt;sex.&apos;生，考了：&apos;.$this-&gt;score.&apos;分数&apos;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$stu1 = new Student(&apos;小明&apos;,&apos;男&apos;,98);</div><div class="line">$stu1-&gt;show();</div><div class="line"> ?&gt;</div><div class="line">Notice: Undefined property: Student::$name in /Users/uiste/www/test.php on line 135</div><div class="line">Notice: Undefined property: Student::$sex in /Users/uiste/www/test.php on line 135</div><div class="line">是生，考了：98分数</div><div class="line">父类是private私有的不能被继承下来；</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">class Person&#123;</div><div class="line">	protected $name;</div><div class="line">	protected $sex;</div><div class="line">	public function __construct($name,$sex)&#123;</div><div class="line">		$this-&gt;name = $name;</div><div class="line">		$this-&gt;sex = $sex;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class Student extends Person&#123;</div><div class="line">	private $score;</div><div class="line">	public function __construct($name,$sex,$score)&#123;</div><div class="line">		parent::__construct($name,$sex);</div><div class="line">		$this-&gt;score = $score;</div><div class="line">	&#125;</div><div class="line">	public function show()&#123;</div><div class="line">		echo $this-&gt;name.&apos;是&apos;.$this-&gt;sex.&apos;生，考了：&apos;.$this-&gt;score.&apos;分数&apos;;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">$stu1 = new Student(&apos;小明&apos;,&apos;男&apos;,98);</div><div class="line">$stu1-&gt;show();</div><div class="line">?&gt;</div><div class="line">小明是男生，考了：98分数</div></pre></td></tr></table></figure>
<blockquote>
<p>protected 修饰的成员在整个继承连上访问<br>$this 表示当前对象的引用(地址)</p>
</blockquote>
<p>A类                B类<br>private         不可访问<br>protected        不可访问<br>public            可以访问</p>
<p>父类             子类</p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/20160911-1.html" itemprop="url">
                  Day16-会话控制、cookie、sessions
                </a>
              
            
          </h1>
        

        <div class="post-meta">
        
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-09-11T14:09:59+08:00" content="2016-09-11">
              2016-09-11
            </time>
          </span>

          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/20160911-1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="20160911-1.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="HTTP协议"><a href="#HTTP协议" class="headerlink" title="HTTP协议"></a>HTTP协议</h3><p>HTTP是无状态的协议<br>HTTP协议没有内建机制来维持两个事务间的状态，无法判断在请求一个页面后再请求另外一个页面时是来自同一用户。</p>
<p>会话控制的思想是指能够在网站中根据一个会话跟踪用户。</p>
<blockquote>
<p>作用：用户登录的支持<br>根据授权级别和个人喜好显示相应的内容。<br>实现购物车功能</p>
</blockquote>
<p>实现原理：<br>通过一个唯一会话ID来驱动的。会话ID是一个加密的随机数字。由PHP生成，在会话的生命周期中都会保存在客户端。保存在用户机器里的cookie中，通过URL在网络上传递。</p>
<h3 id="经典方案"><a href="#经典方案" class="headerlink" title="经典方案"></a>经典方案</h3><p>上次登录时间<br>上次浏览信息<br>规定时间内，不用反复登录</p>
<h3 id="cookie"><a href="#cookie" class="headerlink" title="cookie"></a>cookie</h3><p>如果没有设置时间，cookie不会保存到客户端，浏览器的会话结束，我们的cookie就失效了<br>cookie只能保存字符串 《在此电脑记住用户名》<br>对于中文，cookie会进行urlencode编码，我们可以使用urldecode解码<br>cookie保存的时间可以不一样，虽然是在一个文件内<br>cookie信息是通过http协议传递的</p>
<p>如果需要保存密码，最好使用<strong>md5</strong>加密</p>
<h3 id="增加、更新、删除"><a href="#增加、更新、删除" class="headerlink" title="增加、更新、删除"></a>增加、更新、删除</h3><p><code>setcookie(&#39;username&#39;,&#39;uiste&#39;,time()+60);</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">if (isset($_COOKIE[&quot;user&quot;])) &#123;</div><div class="line">	echo &quot;当前COOKIE是：&quot;.$_COOKIE[&quot;user&quot;];</div><div class="line">	echo &quot;正在清楚cookie&lt;br&gt;&quot;;</div><div class="line">	setcookie(&quot;user&quot;,&quot;uiste&quot;,time()-3600);</div><div class="line">&#125;else&#123;</div><div class="line">	echo &quot;您设置的COOKIE时间已超时&lt;br&gt;&quot;;</div><div class="line">	echo &quot;正在创建COOKIE&lt;br&gt;&quot;;</div><div class="line">	setcookie(&quot;user&quot;,&quot;uiste&quot;,time()+5);</div><div class="line">	echo &quot;cookie 创建成功有效时间为5秒&lt;br&gt;&quot;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">date_default_timezone_set(&quot;Asia/Shanghai&quot;);</div><div class="line">if (isset($_COOKIE[&apos;time&apos;])) &#123;</div><div class="line">	echo &quot;您上次登录的时间是&quot;.$_COOKIE[&apos;time&apos;];</div><div class="line">	setcookie(&apos;time&apos;,Date(&quot;Y-m-d H:i:s&quot;,time()));</div><div class="line">&#125;else&#123;</div><div class="line">	echo &quot;您是第一次登录，登录时间：&quot;.Date(&apos;Y-m-d H:i:s&apos;,time());</div><div class="line">	setcookie(&apos;time&apos;,Date(&quot;Y-m-d H:i:s&quot;,time()));</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="session"><a href="#session" class="headerlink" title="session"></a>session</h3><p>实现简单的会话</p>
<p>开始一个会话<br>注册会话变量<br>使用会话变量<br>注销变量并销毁会话</p>
<p>作用：</p>
<blockquote>
<p>购物车<br>用户信息<br>一个数据供同一用户不同页面使用<br>防止用户非法登录到某个页面</p>
</blockquote>
<h3 id="session可以保存的数据类型"><a href="#session可以保存的数据类型" class="headerlink" title="session可以保存的数据类型"></a>session可以保存的数据类型</h3><p>整形、浮点型、字符串、布尔、数组、对象</p>
<h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><p>要使用session都需要初始化<br>key不能重复<br>如果要取出对象，需要事先声明类的定义<br>session文件创建是在session_start()执行后立即创建；</p>
<h3 id="配置文件"><a href="#配置文件" class="headerlink" title="配置文件"></a>配置文件</h3><p>《手册-附录-配置选项》<br>php.ini配置文件中有一组会话配置选项，可以对其进行设置。如下：<br>session.auto_start = 0 ; 在请求启动时初始化session<br>session.cache_expire = 180 ; 设置缓存中的会话文档在 n 分钟后过时<br>session.cookie_lifetime = 0 ; 设置按秒记的cookie的保存时间， 相当于设置Session的过期时间，为0时表示直到浏览器被重启<br>session.cookie_path = / ; cookie的有效路径<br>session.cookie_domain = ; cookie的有效域<br>session.name = PHPSESSID；  用在cookie里的session的名字<br>session.save_handler = files ; 用于保存/取回数据的控制方式<br>session.save_path = /tmp ; 在 save_handler 设为文件时传给控制器的参数， 这是数据文件将保存的路径.<br>session.use_cookies = 1 ; 是否使用cookies </p>
<p>需要重启Apache</p>
<blockquote>
<p>这些步骤不一定都要发生在同一个脚本中，其中</p>
</blockquote>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">&lt;form action=<span class="string">""</span> method=<span class="string">"post"</span>&gt;</div><div class="line">		用户名：&lt;input type=<span class="string">"text"</span> name=<span class="string">"username"</span>&gt;&lt;br&gt;</div><div class="line">		密　码: &lt;input type=<span class="string">"password"</span> name=<span class="string">"password"</span>&gt;&lt;br&gt;</div><div class="line">		&lt;input type=<span class="string">"submit"</span> value=<span class="string">"提交"</span>&gt;</div><div class="line">	&lt;/form&gt;</div><div class="line">	<span class="meta">&lt;?php</span> </div><div class="line">	<span class="keyword">if</span> ($_POST) &#123;</div><div class="line">		$username = $_POST[<span class="string">'username'</span>];</div><div class="line">		$password = mysql_real_escape_string($_POST[<span class="string">'password'</span>]);</div><div class="line">		<span class="keyword">require</span> <span class="string">'../inc/conn.php'</span>;</div><div class="line">		$sql = <span class="string">"select * from cookie where username='$username' and password='$password'"</span>;</div><div class="line">		$res = mysql_query($sql);</div><div class="line">		<span class="keyword">if</span> (mysql_fetch_assoc($res)) &#123;</div><div class="line">			<span class="keyword">echo</span> <span class="string">"登录成功"</span>;</div><div class="line">			<span class="comment">//登录成功就设置session</span></div><div class="line">			session_start();</div><div class="line">			$_SESSION[<span class="string">'username'</span>]=$username;</div><div class="line">			$_SESSION[<span class="string">'password'</span>]=$password;</div><div class="line">			<span class="keyword">echo</span> $_SESSION[<span class="string">'username'</span>];</div><div class="line">		&#125;<span class="keyword">else</span>&#123;</div><div class="line">			<span class="keyword">echo</span> <span class="string">'用户名或密码错误，请重新登录'</span>;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	 <span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span> </div><div class="line">header(<span class="string">"content-type:text/html;charset=utf8"</span>);</div><div class="line">session_start();</div><div class="line"><span class="keyword">if</span> (<span class="keyword">empty</span>($_SESSION[<span class="string">'username'</span>])) &#123;</div><div class="line">	<span class="keyword">echo</span> <span class="string">"你没有登录请重新登录&lt;br&gt;"</span>;</div><div class="line">	<span class="keyword">echo</span> <span class="string">"&lt;a href='hw1_form.php'&gt;登录&lt;/a&gt;"</span>;</div><div class="line">&#125;<span class="keyword">else</span>&#123;</div><div class="line">	<span class="keyword">echo</span> <span class="string">"可以购物"</span>;</div><div class="line">	<span class="keyword">echo</span> <span class="string">"&lt;hr&gt;以下内容是退出功能的实现"</span>;</div><div class="line">	<span class="keyword">echo</span> <span class="string">"&lt;hr&gt;兄弟不好意思，既然成功了我就要给你清除session了&lt;br&gt;"</span>;</div><div class="line">	<span class="keyword">echo</span> <span class="string">'清除session一共两步&lt;br&gt;第一步：注销变量&lt;br&gt;第二步：销毁会话&lt;br&gt;'</span>;</div><div class="line">	<span class="keyword">unset</span>($_SESSION[<span class="string">'username'</span>]);</div><div class="line">	session_destroy();</div><div class="line">&#125;</div><div class="line"> <span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>session与cookie的区别<br>cookie是把用户的信息写给用户的浏览器<br>session是把用户的数据写到用户都占的<code>$_SESSION</code>中，存在服务器的某个路径文件中，只用<code>session</code>时要先初始化<code>session_start()</code>;<br>php.ini-&gt;<code>session.auto_start=1</code> 自动开启session不推荐</p>
<p>如果客户端禁用cookie怎样实现session多页面共享<br>在每个超链接上添加一个PHPSESSID=sessionid；<br>同时在每个页面加入：<br>if(isset($_GET[‘PHPSESSID’])){<br>    //设置session_id;<br>    session_id($_GET[‘PHPSESSID’]);<br>}</p>
<p>session_start();</p>
<p>使用系统内置常量：<code>SID</code>;</p>
<p>echo “<a href="a.php?".SID.""></a>“</p>
<blockquote>
<p>以上解决方案为URL重写</p>
</blockquote>
<p>解决非法登录问题<br>if($name!=’’){<br>    session_start();<br>    $_SESSION[‘username’]=$name;<br>    header(“Location:登录成功页?name=$name”);<br>}</p>
<p>if(empty($_SESSION[‘username’])){<br>    header(‘Location:login.php?error=1’);<br>}</p>
<blockquote>
<p>垃圾回收机制<br>cookie默认关闭浏览器即失效</p>
</blockquote>
<p>cookie的生命周期是固定的<br>session的生命周期是访问即重新计算</p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/20160910-Library-php-fun.html" itemprop="url">
                  史上最全、最详细的PHP函数详解
                </a>
              
            
          </h1>
        

        <div class="post-meta">
        
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-09-10T09:31:03+08:00" content="2016-09-10">
              2016-09-10
            </time>
          </span>

          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/20160910-Library-php-fun.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="20160910-Library-php-fun.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>必读PHP函数<br>请参考手册</p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/5/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/5/">5</a><span class="page-number current">6</span><a class="page-number" href="/page/7/">7</a><span class="space">&hellip;</span><a class="page-number" href="/page/14/">14</a><a class="extend next" rel="next" href="/page/7/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel  sidebar-panel-active ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="uiste" />
          <p class="site-author-name" itemprop="name">uiste</p>
          <p class="site-description motion-element" itemprop="description">Stay Hungry，Stay Foolish</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">70</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">55</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/uiste" target="_blank" rel="external nofollow" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/uiste" target="_blank" rel="external nofollow" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/uiste" target="_blank" rel="external nofollow" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              友情链接
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.uiste.com" title="uiste 个人博客" target="_blank">uiste 个人博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://uiste.github.io" title="uiste 技术博客" target="_blank">uiste 技术博客</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">uiste</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io" rel="external nofollow">Hexo</a> 趋势动力
</div>
<!--站长统计-->
<script src="https://s95.cnzz.com/z_stat.php?id=1259894121&web_id=1259894121" language="JavaScript"></script>
<!--<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">
    NexT.Mist
  </a>
</div> -->
&nbsp;&nbsp;&nbsp;本站总点击 <span id="busuanzi_value_site_pv"></span> 次
&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;您是第 <span id="busuanzi_value_site_uv"></span> 位访客

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>

<script>
(function(){
    var bp = document.createElement('script');
    bp.src = '//push.zhanzhang.baidu.com/push.js';
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"uiste"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
       search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();

    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
    'use strict';
    $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
            // get the contents from search data
            isfetched = true;
            $('.popup').detach().appendTo('.header-inner');
            var datas = $( "entry", xmlResponse ).map(function() {
                return {
                    title: $( "title", this ).text(),
                    content: $("content",this).text(),
                    url: $( "url" , this).text()
                };
            }).get();
            var $input = document.getElementById(search_id);
            var $resultContent = document.getElementById(content_id);
            $input.addEventListener('input', function(){
                var matchcounts = 0;
                var str='<ul class=\"search-result-list\">';
                var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                $resultContent.innerHTML = "";
                if (this.value.trim().length > 1) {
                // perform local searching
                datas.forEach(function(data) {
                    var isMatch = true;
                    var content_index = [];
                    var data_title = data.title.trim().toLowerCase();
                    var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                    var data_url = data.url;
                    var index_title = -1;
                    var index_content = -1;
                    var first_occur = -1;
                    // only match artiles with not empty titles and contents
                    if(data_title != '' && data_content != '') {
                        keywords.forEach(function(keyword, i) {
                            index_title = data_title.indexOf(keyword);
                            index_content = data_content.indexOf(keyword);
                            if( index_title < 0 && index_content < 0 ){
                                isMatch = false;
                            } else {
                                if (index_content < 0) {
                                    index_content = 0;
                                }
                                if (i == 0) {
                                    first_occur = index_content;
                                }
                            }
                        });
                    }
                    // show search results
                    if (isMatch) {
                        matchcounts += 1;
                        str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                        var content = data.content.trim().replace(/<[^>]+>/g,"");
                        if (first_occur >= 0) {
                            // cut out 100 characters
                            var start = first_occur - 20;
                            var end = first_occur + 80;
                            if(start < 0){
                                start = 0;
                            }
                            if(start == 0){
                                end = 50;
                            }
                            if(end > content.length){
                                end = content.length;
                            }
                            var match_content = content.substring(start, end);
                            // highlight all keywords
                            keywords.forEach(function(keyword){
                                var regS = new RegExp(keyword, "gi");
                                match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                            });

                            str += "<p class=\"search-result\">" + match_content +"...</p>"
                        }
                        str += "</li>";
                    }
                })};
                str += "</ul>";
                if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
                if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
                $resultContent.innerHTML = str;
            });
            proceedsearch();
        }
    });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };

    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

</body>
</html>
